package com.xmx.authcenter.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;

import cn.hutool.core.map.MapUtil;
import com.xmx.authcenter.constant.MessageConstant;
import com.xmx.authcenter.dto.SecurityUser;
import com.xmx.authcenter.dto.UserDTO;
import com.xmx.authcenter.feign.MemeberFeignClient;
import com.xmx.common.utils.ListFromEntity2VoUtils;
import com.xmx.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AccountExpiredException;
import org.springframework.security.authentication.CredentialsExpiredException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户管理业务类
 * Created by macro on 2020/6/19.
 */
@Service
@Slf4j
public class UserServiceImpl implements UserDetailsService {

    private List<UserDTO> userList;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Resource
    MemeberFeignClient memeberFeignClient;

    @PostConstruct
    public void initData() {
        String password = passwordEncoder.encode("o6mgh5WWjr_8Dpur6S2YDmK_ZkPk");
        userList = new ArrayList<>();
        userList.add(new UserDTO(1,"macro", password,1, CollUtil.toList("ADMIN")));
        userList.add(new UserDTO(2,"andy", password,1, CollUtil.toList("TEST")));
        userList.add(new UserDTO(2,"o6mgh5WWjr_8Dpur6S2YDmK_ZkPk",password ,1, CollUtil.toList("ADMIN")));
    }


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        List<UserDTO> findUserList = userList.stream().filter(item -> item.getUsername().equals(username)).collect(Collectors.toList());
        R userInfoR = memeberFeignClient.getUserInfoByUsername(username);

        List<Map> userInfoMaplist = MapUtil.get( userInfoR,"data",List.class);

        if (CollUtil.isEmpty(userInfoMaplist)) {
            throw new UsernameNotFoundException(MessageConstant.USERNAME_PASSWORD_ERROR);
        }
        UserDTO userList = new UserDTO((Integer) userInfoMaplist.get(0).get("id"), (String) userInfoMaplist.get(0).get("username"),(String)userInfoMaplist.get(0).get("password"), (Integer) userInfoMaplist.get(0).get("status"),CollUtil.toList("TEST"));
        log.error("========================>>{}",userList);
        SecurityUser securityUser = new SecurityUser(userList);
        if (!securityUser.isEnabled()) {
            throw new DisabledException(MessageConstant.ACCOUNT_DISABLED);
        } else if (!securityUser.isAccountNonLocked()) {
            throw new LockedException(MessageConstant.ACCOUNT_LOCKED);
        } else if (!securityUser.isAccountNonExpired()) {
            throw new AccountExpiredException(MessageConstant.ACCOUNT_EXPIRED);
        } else if (!securityUser.isCredentialsNonExpired()) {
            throw new CredentialsExpiredException(MessageConstant.CREDENTIALS_EXPIRED);
        }
        return securityUser;
    }

}
